﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Collections;
using GPCSC.Web;
using NickLee.Common;

namespace GPCSC.Web.SYS_ORG
{
    public partial class dwUserList : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                f_buildGrid();
            }
        }
        protected void f_buildGrid()
        {
            Hashtable htable = new Hashtable();
            byte[] userInfo = (byte[])base.o_baseTable[c_obj.s_userInfo];
            Hashtable userInfoTable = CompressData.UnCompressHashtable(userInfo);
            Model.CL_USER_S users = (Model.CL_USER_S)userInfoTable["SYS_USER"];
            htable.Add("@CL_POSTID_PK",users.CL_POSTID_PK.ToString());
            DataSet ds = new DataReturn().ReturnData(htable, "sel_user_bypost");

            if (ds != null)
            {
                if (ds.Tables[0].Rows.Count > 0)
                {
                    foreach (DataRow dbRow in ds.Tables[0].Rows)
                    {

                        if (dbRow["CL_POSTID_PK"].ToString() == users.CL_POSTID_PK.ToString())
                        {
                            if (dbRow["EditUrl"] != null)
                            {
                                dbRow["EditUrl"] = "dwUserEdit.aspx?Command=Edit";
                            }

                            if (dbRow["ViewUrl"] != null)
                            {
                                dbRow["ViewUrl"] = "dwUserEdit.aspx?Command=View";
                            }
                            if (dbRow["CL_ROLEOID_PK"].ToString() != "1224a88c-59fb-42c0-ae2e-b5a2f623b653")
                            {
                                dbRow["iddw"] = "<strong style='color:red;'>管理员</strong>";
                            }
                            else
                            {
                                dbRow["iddw"] = "<strong style='color:red;'>领导干部</strong>";
                            }
                            if (dbRow["cz"] != null)
                            {
                                dbRow["cz"] = "[修改]";
                            }
                        }
                        else
                        {
                            if (dbRow["EditUrl"] != null)
                            {
                                dbRow["EditUrl"] = "1";
                            }

                            if (dbRow["ViewUrl"] != null)
                            {
                                dbRow["ViewUrl"] = "dwUserEdit.aspx?Command=View";
                            }
                            if (dbRow["CL_ROLEOID_PK"].ToString() == "79fa835b-613c-459a-b56e-2c26f8812918")
                            {
                                dbRow["iddw"] = "<strong style='color:red;'>管理员</strong>";
                            }
                            else
                            {
                                dbRow["iddw"] = "<strong style='color:red;'>领导干部</strong>";
                            }
                            if (dbRow["cz"] != null)
                            {
                                dbRow["cz"] = "[设为单位管理员]";
                            }
                        }
                    }
                }
                PostListS.DataSource = ds;
                PostListS.DataBind();
                this.pageinfo.InnerHtml = new FYpage().GetPageNum(ds.Tables[0], PostListS, 10);

            }
        }
        /// <summary>
        /// Callback异步加载数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void LCallBack_Callback(object sender, NickLee.Web.UI.CallBackEventArgs e)
        {
            SqlReadXml org = new SqlReadXml();
            try
            {
                #region 删除数据
                if (Usual.f_convertString(e.Parameters[0]) == "3")
                {
                    SqlReadXml Org = new SqlReadXml();
                    var allname = "";
                    var deptname = "";
                    try
                    {

                        var zid = Usual.f_convertString(e.Parameters[2]);
                        if (zid == "" || zid == null)
                        {

                        }
                        base.o_uiTable.Clear();
                        base.o_uiTable.Add("@USERID", zid);

                        //先查找有无子部门


                        //删除此部门
                        if (deptname == "")
                        {
                            Org.NotDoSQL("del_user_one", base.o_uiTable);
                            hfError.Value = lblProcesserMsg.Text = "删除数据成功!";
                        }
                        this.f_buildGrid();
                    }
                    catch (System.Exception ee)
                    {
                        hfError.Value = lblProcesserMsg.Text = "删除数据失败!" + ee.Message;

                    }
                }

                else if (Usual.f_convertString(e.Parameters[0]) == "2")
                {
                    SqlReadXml Org = new SqlReadXml();//
                    var zid = Usual.f_convertString(e.Parameters[2]);
                    if (zid == "" || zid == null)
                    {
                        this.f_buildGrid();
                    }
                    else
                    {
                          Hashtable htable = new Hashtable();
                        byte[] userInfo = (byte[])base.o_baseTable[c_obj.s_userInfo];
                        Hashtable userInfoTable = CompressData.UnCompressHashtable(userInfo);
                        Model.CL_USER_S users = (Model.CL_USER_S)userInfoTable["SYS_USER"];
                        string likes = e.Parameters[2].ToString().Trim();
                        likes = "%" + likes + "%";
                   
                        htable.Add("@like", likes);
                        htable.Add("@CL_POSTID_PK",users.CL_POSTID_PK.ToString());
                        DataSet ds = Org.ReaderDataSet("sel_dwuser_bylike", htable);
                        if (ds != null)
                        {
                            if (ds.Tables[0].Rows.Count > 0)
                            {
                                foreach (DataRow dbRow in ds.Tables[0].Rows)
                                {

                                    if (dbRow["CL_POSTID_PK"].ToString() == users.CL_POSTID_PK.ToString())
                                    {
                                        if (dbRow["EditUrl"] != null)
                                        {
                                            dbRow["EditUrl"] = "dwUserEdit.aspx?Command=Edit";
                                        }

                                        if (dbRow["ViewUrl"] != null)
                                        {
                                            dbRow["ViewUrl"] = "dwUserEdit.aspx?Command=View";
                                        }
                                        if (dbRow["CL_ROLEOID_PK"].ToString() != "1224a88c-59fb-42c0-ae2e-b5a2f623b653")
                                        {
                                            dbRow["iddw"] = "<strong style='color:red;'>管理员</strong>";
                                        }
                                        else
                                        {
                                            dbRow["iddw"] = "<strong style='color:red;'>领导干部</strong>";
                                        }
                                        if (dbRow["cz"] != null)
                                        {
                                            dbRow["cz"] = "[修改]";
                                        }
                                    }
                                    else
                                    {
                                        if (dbRow["EditUrl"] != null)
                                        {
                                            dbRow["EditUrl"] = "1";
                                        }

                                        if (dbRow["ViewUrl"] != null)
                                        {
                                            dbRow["ViewUrl"] = "dwUserEdit.aspx?Command=View";
                                        }
                                        if (dbRow["CL_ROLEOID_PK"].ToString() == "79fa835b-613c-459a-b56e-2c26f8812918")
                                        {
                                            dbRow["iddw"] = "<strong style='color:red;'>管理员</strong>";
                                        }
                                        else
                                        {
                                            dbRow["iddw"] = "<strong style='color:red;'>领导干部</strong>";
                                        }
                                        if (dbRow["cz"] != null)
                                        {
                                            dbRow["cz"] = "[设为单位管理员]";
                                        }
                                    }
                                }
                            }
                            PostListS.DataSource = ds;
                            PostListS.DataBind();
                            this.pageinfo.InnerHtml = new FYpage().GetPageNum(ds.Tables[0], PostListS, 10);

                        }

                    }
                }
                #endregion

                #region   设为单位管理员

                else if (Usual.f_convertString(e.Parameters[0]) == "4")
                {
                    string userid = e.Parameters[1].ToString();
                    Hashtable hat = new Hashtable();
                    hat.Add("state",1);
                    SQLServerDAL.databyhtable dtable = new SQLServerDAL.databyhtable();
                    dtable.databyhtables(hat, "update", "CL_USER_S", "CL_USER_SOID", userid);
                    f_buildGrid();
                }

                #endregion

                #region  上移
                if (e.Parameters[0].ToString() == "up")
                {
                    int number = Convert.ToInt32(Usual.f_convertString(e.Parameters[1]));
                    if (number != 1)
                    {
                         Hashtable htable = new Hashtable();
                        byte[] userInfo = (byte[])base.o_baseTable[c_obj.s_userInfo];
                        Hashtable userInfoTable = CompressData.UnCompressHashtable(userInfo);
                        Model.CL_USER_S users = (Model.CL_USER_S)userInfoTable["SYS_USER"];
                        int upnumber = number - 1;
                        Hashtable htab = new Hashtable();
                        htab.Add("@number",number);
                        htab.Add("@upnumber",upnumber);
                        htab.Add("@CL_POSTID_PK", users.CL_POSTID_PK);
                        int jj = org.NotDoSQL("beup_userNumber", htab);
                        if (jj == 1)
                        {
                            Guid userid = new Guid(Usual.f_convertString(e.Parameters[2]));
                            Hashtable ht = new Hashtable();
                            ht.Add("@upnumber", upnumber);
                            ht.Add("@CL_USER_SOID", userid);
                            //此人顺序上升
                            int ii = org.NotDoSQL("up_userNumber", ht);
                        }

                    }
                    this.f_buildGrid();
                }
                #endregion 
                if (e.Parameters[0].ToString() == "down")
                {
                    
                    byte[] userInfo = (byte[])base.o_baseTable[c_obj.s_userInfo];
                        Hashtable userInfoTable = CompressData.UnCompressHashtable(userInfo);
                        Model.CL_USER_S users = (Model.CL_USER_S)userInfoTable["SYS_USER"];
                    Hashtable htba = new Hashtable();
                    htba.Add("@CL_POSTID_PK",users.CL_POSTID_PK.ToString());

                    DataSet max = org.ReaderDataSet("selmaxsel", htba);

                    int mads =Usual.f_convertInt32(max.Tables[0].Rows[0][0].ToString());
                    int number = Convert.ToInt32(Usual.f_convertString(e.Parameters[1]));
                    if (number < mads)
                    {
                        int down = number + 1;
                        htba.Clear();
                        htba.Add("@number", number);
                        htba.Add("@upnumber", down);
                        htba.Add("@CL_POSTID_PK", users.CL_POSTID_PK);
                        int jj = org.NotDoSQL("beup_userNumber", htba);
                        if (jj == 1)
                        {
                            Guid userid = new Guid(Usual.f_convertString(e.Parameters[2]));
                            Hashtable ht = new Hashtable();
                            ht.Add("@upnumber", down);
                            ht.Add("@CL_USER_SOID", userid);
                            //此人顺序下升
                            int ii = org.NotDoSQL("up_userNumber", ht);
                        }
                    }
                    this.f_buildGrid();
                }
            }
            catch (System.Exception pexp)
            {
                throw (pexp);
            }
            //最后进行Grid数据查询并填充
            finally
            {

                LPanel.RenderControl(e.Output);
            }
        }
    }
}